. ./tuna.sh

# check for dependencies:
err_depend=false
if [ -z "$(command -v awk)" ]; then
	log ERROR "awk not installed!\n"
	err_depend=true
fi

if [ -z "$(command -v sed)" ]; then
	log ERROR "sed not installed!\n"
	err_depend=true
fi

if [ -z "$(command -v curl)" ]; then
	log ERROR "curl not installed!\n"
	err_depend=true
fi

if [ -z "$(command -v jq)" ]; then
	log ERROR "jq not installed!\n"
	err_depend=true
fi

if [ -z "$(command -v ip)" ]; then
	log WARNING "ip not installed!\n"
fi

if [ -z "$(command -v cgexec)" ]; then
	log WARNING "cgroups not installed!\n"
fi

if $err_depend; then
	exit 1
fi

image_name=""
image_tag="latest"
image_newname=""
ASK=false

print_help() {
	printf '%b' "HELP\n----------------------\n"
	printf '%b' "\033[35;1m-a\033[0m\t: Ask before removing images\n"
	printf '%b' "\033[35;1m-h\033[0m\t: Show help\n"
	printf '%b' "\033[35;1m-H\033[0m\t: Show help and examples\n"
	printf '%b' "\033[35;1m-i\033[0m\t: Set Image Name (distribution)\n"
	printf '%b' "\033[35;1m-n\033[0m\t: Set Can Name\n"
	printf '%b' "\033[35;1m-t\033[0m\t: Set Image Tag (release)\n"
	printf '%b' "\033[0mFunctions are:\n"
	printf '%b' "\n\033[34;1mlist listcans rm rmcan pull build\n"
	printf '%b' "\n\033[0mCheck \033[35;1m-H\033[0m for examples!\n"
	printf '%b' "\033[0m"
}

print_examples() {
	printf '%b' "\nEXAMPLES\n----------------------\n"
	printf '%b' "\033[32;1m-- Pull an Ubuntu bionic image\033[0m\n"
	printf '%b' "run_tuna\033[35;1m -a -i\033[0m ubuntu \033[35;1m-t bionic\033[0m \033[34;1mpull\n"
	printf '%b' "\033[32;1m-- Remove Ubuntu bionic image from disk\033[0m\n"
	printf '%b' "run_tuna\033[35;1m -a -i\033[0m ubuntu \033[35;1m-t bionic\033[0m \033[34;1mrm\n"
	printf '%b' "\033[32;1m-- Build an Ubuntu Bionic Chroot\033[0m\n"
	printf '%b' "run_tuna\033[35;1m -a -i\033[0m ubuntu \033[35;1m-t bionic\033[0m \033[34;1mbuild\n"
	printf '%b' "\033[32;1m-- List all installed images\033[0m\n"
	printf '%b' "run_tuna \033[34;1mlist\n"
	
	printf '%b' "\033[32;1m-- Multiple operations are supported\033[0m\n"
	printf '%b' "run_tuna\033[35;1m -a -i\033[0m ubuntu \033[35;1m-t bionic\033[0m \033[34;1mpull build\n"
	printf '%b' "\033[0m"
}

while getopts 'hHi:t:an:' arg; do
	case "$arg" in
		h)
			print_help
			exit 0
			;;
		H)
			print_examples
			exit 0
			;;
		i)
			image_name="$OPTARG"
			;;
		t)
			image_tag="$OPTARG"
			;;
		n)
			image_newname="$OPTARG"
			;;
		a)
			export ASK=true
			;;
		*)
			print_help
			print_examples
			exit 0
			;;
	esac
done
if ! [ $# -gt 0 ]; then
	print_help
	print_examples 
	exit 0
fi

shift $(expr $OPTIND - 1)
if ! [ "$1" = "list" ] && ! [ "$1" = "listcans" ] && ! [ "$1" = "rmcan" ]; then
	if [ -z "$image_name" ]; then
		echo "No name specified."
		exit 1
	fi

	if [ -z "$(echo "$image_name" | grep '/' )" ]; then
		image_name="library/$image_name"
	fi
fi

if [ -z "$image_tag" ]; then
	image_tag="latest"
fi

while [ "$#" -gt 0 ]; do
	case "$1" in
		pull)
			IMAGE="$image_name"
			TAG="$image_tag"
			DIR='./tunafiles/'
	
			mkdir -p $DIR/$IMAGE/$TAG

			TOKEN="$(get_token $IMAGE)"
			MANIFEST="$(fetch_manifest $TOKEN $IMAGE $TAG)"
			handle_manifest_v2 "$TOKEN" "$IMAGE" "$TAG" "$DIR" "$MANIFEST"
			;;
		rm)
			if $ASK; then
				log INFO "Image $image will be removed [y/n]: "
				read askresult
				case "$askresult" in
					y|Y|yes|YES|Yes)
						rm -rf "./tunafiles/$image_name/$image_tag"
						;;
					*)
						exit 0
						;;
				esac
			else
				rm -rf ./tunafiles/$image_name/$image_tag
			fi
			;;
		rmcan)
			if $ASK; then
				log INFO "Image $image will be removed [y/n]: "
				read askresult
				case "$askresult" in
					y|Y|yes|YES|Yes)
						sudo rm -rf "./tunafiles/cans/$image_newname/" "./tunafiles/cans/$image_newname.json"
						;;
					*)
						exit 0
						;;
				esac
			else
					sudo rm -rf "./tunafiles/cans/$image_newname/" "./tunafiles/cans/$image_newname.json"
			fi
			;;
		build)
			IMAGE="$image_name"
			TAG="$image_tag"
			NAME="$image_newname"
			DIR='./tunafiles/'

			if [ -z "$NAME" ]; then
					NAME="$(echo $IMAGE | cut -f2 -d/)_$TAG"
			fi

			mkdir -p $DIR

			if ! [ -d "$DIR/$IMAGE/$TAG" ]; then
				log ERROR "No Image $IMAGE:$TAG found!\n"
				exit 1
			fi

			#RESULT_PATH="$DIR/cans/$(echo $IMAGE | cut -f2 -d/)/$TAG"
			RESULT_PATH="$DIR/cans/$NAME"
			mkdir -p "$RESULT_PATH"

			log WARNING "The following command has to be executed as root\n"
			log WARNING "This is to ensure correct permissions on extracted files.\n"
			for x in $(ls -1 "$DIR/$IMAGE/$TAG"); do
				log INFO "Extracting $DIR/$IMAGE/$TAG/$x\n"
				sudo tar -xf "$DIR/$IMAGE/$TAG/$x/layer.tar.gz" -C "$RESULT_PATH"
			done
			cp "$DIR/$IMAGE/$TAG.json" "$(dirname $RESULT_PATH)/$NAME.json"
			jq -cr ". + {\"canned\": \"$(date +%Y-%m-%d)\"}" "$(dirname $RESULT_PATH)/$NAME.json" > "$(dirname $RESULT_PATH)/$NAME.json.tmp" && mv "$(dirname $RESULT_PATH)/$NAME.json.tmp" "$(dirname $RESULT_PATH)/$NAME.json"
			jq -cr ". + {\"origin\": \"$IMAGE/$TAG\"}" "$(dirname $RESULT_PATH)/$NAME.json" > "$(dirname $RESULT_PATH)/$NAME.json.tmp" && mv "$(dirname $RESULT_PATH)/$NAME.json.tmp" "$(dirname $RESULT_PATH)/$NAME.json"
			log SUCCESS "chroot is available at:\n"
			log SUCCESS "$RESULT_PATH/\n"
			;;
		list)
			DIR='./tunafiles/'

			if ! [ -d "$DIR/library/" ]; then
				log OPTINFO "No images installed :("
				exit 0
			fi

			log INFO "$(printf '\033[4;1m  %-12s %-8s %-4s %-10s %-3s\033[0m' \
				"DISTRO" "TAG" "SIZE" "PULLED" "CAN")\n"

			for x in $(ls -1 "$DIR/library/"); do
				for y in $(ls -1 "$DIR/library/$x"); do
					if [ -d "$DIR/library/$x/$y" ]; then
						canned="---"
						if [ -d "$DIR/cans/$x/$y" ]; then
							canned="YES"
						else
							canned="NO"
						fi
						icon=""

						case "$x" in
							ubuntu)
								icon="\033[1m\033[0m"
								;;
							fedora)
								icon="\033[1m\033[0m"
								;;
							alpine)
								icon="\033[1m\033[0m"
								;;
							gentoo)
								icon="\033[1m\033[0m"
								;;
							arch)
								icon="\033[1m\033[0m"
								;;
							*)
								icon="\033[1m\033[0m"
								;;
						esac

						size="$(du -sh $DIR/library/$x/$y | sed 's/\n//; s/\t.*//')"
						pulldate="$(jq -r .pulled "$DIR/library/$x/$y.json")"

						log INFO "$(printf '%b %-12s %-8s %-4s %-10s %-3s' \
							"$icon" "$x" "$y" "$size" "$pulldate" "$canned")\n"
					fi
				done
			done
			;;
		listcans)
			DIR='./tunafiles/'

			if ! [ -d "$DIR/cans" ]; then
				log OPTINFO "No cans were built :("
				exit 0
			fi

			log INFO "$(printf '\033[4;1m  %-12s %-16s %-4s %-10s\033[0m' \
				"DISTRO" "ORIGIN" "SIZE" "CREATED")\n"

			for x in $(ls -1 "$DIR/cans/"); do
				if [ -d "$DIR/cans/$x" ]; then
					icon=""

					case "$x" in
						ubuntu)
							icon="\033[1m\033[0m"
							;;
						fedora)
							icon="\033[1m\033[0m"
							;;
						alpine)
							icon="\033[1m\033[0m"
							;;
						gentoo)
							icon="\033[1m\033[0m"
							;;
						arch)
							icon="\033[1m\033[0m"
							;;
						*)
							icon="\033[1m\033[0m"
							;;
					esac

					size="$(du -sh $DIR/cans/$x 2>/dev/null | sed 's/\n//; s/\t.*//')"
					createdate="$(jq -r .canned "$DIR/cans/$x.json")"
					origin="$(jq -r .origin "$DIR/cans/$x.json" | sed 's:^[a-zA-Z0-9]*/::' )"

					log INFO "$(printf '%b %-12s %-16s %-4s %-10s' \
						"$icon" "$x" "$origin" "$size" "$createdate")\n"
				fi
			done
			;;
	esac

	shift
done


